
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.Abstract;
using System.Data.Entity;
using DomainModel.Context;

namespace DomainModel.Concrete
{
    public abstract class Repository<T> : IRepository<T> where T : class
    {
        protected readonly IDbSet<T> _dbSet;
        protected readonly DbContext _dbContext;
        public Repository(DbContext dbContext)
        {
            _dbContext = dbContext;
            _dbSet = _dbContext.Set<T>();
        }

        public virtual void Insert(T entity)
        {
            _dbSet.Add(entity);
            _dbContext.SaveChanges();
        }

        public virtual void Update(T entity)
        {
            _dbSet.Attach(entity);
            _dbContext.Entry(entity).State = System.Data.EntityState.Modified;
            _dbContext.SaveChanges();
        }

        public virtual void Delete(T entity)
        {
            _dbSet.Remove(entity);
            _dbContext.SaveChanges();
        }

        public virtual T GetById(int Id)
        {
            return _dbSet.Find(Id);
        }
        public virtual IEnumerable<T> GetAll()
        {
            return _dbSet.ToList();
        }

    }
}
